<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>职位频率动态展示</title>
    <!-- 使用 Axios 来发送请求 -->
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <style>
        table {
            width: 50%;
            margin: 20px auto;
            border-collapse: collapse;
        }
        th, td {
            padding: 10px;
            text-align: left;
            border: 1px solid black;
        }
        th {
            background-color: #f2f2f2;
        }
    </style>
</head>
<body>
    <h1 style="text-align: center;">职位频率动态展示</h1>
    <table id="frequency-table">
        <thead>
            <tr>
                <th>职位</th>
                <th>频率</th>
            </tr>
        </thead>
        <tbody>
            <!-- 动态数据插入 -->
        </tbody>
    </table>

    <script>
        // 定义一个函数用于从服务器获取词频数据
        function fetchJobFrequency() {
            axios.get('/get_job_frequency')
                .then(response => {
                    const data = response.data;
                    const tableBody = document.querySelector('#frequency-table tbody');

                    // 清空现有的表格数据
                    tableBody.innerHTML = '';

                    // 遍历返回的数据并插入到表格中
                    data.forEach(row => {
                        const newRow = `<tr>
                            <td>${row.职位}</td>
                            <td>${row.频率}</td>
                        </tr>`;
                        tableBody.innerHTML += newRow;
                    });
                })
                .catch(error => {
                    console.error('获取数据错误:', error);
                });
        }

        // 页面加载后立即执行一次
        fetchJobFrequency();
        // 每隔 30 秒刷新一次数据
        setInterval(fetchJobFrequency, 30000);
    </script>
</body>
</html>
